package com.yami.shop.user.common.service.impl;

import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.util.PageUtil;
import com.yami.shop.user.common.dao.UserGasRecordMapper;
import com.yami.shop.user.common.dto.UserGasRecordVO;
import com.yami.shop.user.common.model.UserGasAccount;
import com.yami.shop.user.common.model.UserGasRecord;
import com.yami.shop.user.common.service.UserGasAccountService;
import com.yami.shop.user.common.service.UserGasRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class UserGasRecordServiceImpl implements UserGasRecordService {

    @Autowired
    private UserGasRecordMapper userGasRecordMapper;

    @Autowired
    @Lazy
    private UserGasAccountService userGasAccountService;

    @Override
    public PageVO<UserGasRecord> pageGasRecordList(PageDTO pageDTO, String year, String month, String userId) {
        PageVO<UserGasRecord> pagelist = PageUtil.doPage(pageDTO, () -> userGasRecordMapper.selectByMonth(year,month,userId));
        return pagelist;
    }

    @Override
    public void insertGasRecord(UserGasRecordVO userGasAccountVO) {
        UserGasRecord userGasRecord = new UserGasRecord();
        userGasRecord.setCreateTime(new Date());
        UserGasAccount userGasById = userGasAccountService.getUserGasById(userGasAccountVO.getUserId()); //查询剩余次数
        userGasRecord.setSurplusNum(userGasById.getGasNum()); //剩余总次数
        userGasRecord.setUserId(userGasAccountVO.getUserId());
        userGasRecord.setRecordNum(userGasAccountVO.getRecordNum());
        userGasRecord.setType(userGasAccountVO.getType());
        userGasRecord.setActivity(userGasAccountVO.getActivity());
        userGasRecordMapper.insertSelective(userGasRecord);
    }


//    @Override
//    public void getGasNum(Long userId) {
//        int rechargeNum = 0;
//        List<NftRechargeActivity> rechargeInfo = getRechargeInfo();
//        if(getRechargeInfo().size()>0){
//            rechargeNum = rechargeInfo.get(0).getActivityNum();//获取默认次数
//            //查询是否参与过活动
//            List<UserGasRecord> gasRecordList = getGasRecordList(userId,rechargeInfo.get(0).getId());
//            if(gasRecordList.size()>0){
//                throw new MallcloudException("已参加过此活动");
//            }else{
//                ServerResponseEntity<AuthAccountVO> authInfoByUserId = accountFeignClient.getAuthInfoByUserId(userId);
//                if(!authInfoByUserId.isSuccess()){
//                    throw new MallcloudException("获取用户链上地址异常");
//                }
//                String dtcAddress = authInfoByUserId.getData().getDtcdawAddress();  //充值链上地址
//                UserGasAccountVO userGasAccountVO = new UserGasAccountVO();
//                userGasAccountVO.setGasNum(rechargeNum);
//                userGasAccountVO.setUserId(userId);
//                userGasAccountVO.setDtcAddress(dtcAddress);
//                userGasAccountVO.setCreateTime(new Date());
//                //添加用户表能量次数
//                userGasAccountService.saveGasNum(userGasAccountVO);
//                UserGasRecordVO userGasRecordVO = new UserGasRecordVO();
//                userGasRecordVO.setType(4);  // 4活动领取
//                userGasRecordVO.setRecordNum(rechargeNum); //记录能量值
//                userGasRecordVO.setUserId(userId);
//                userGasRecordVO.setActivity(rechargeInfo.get(0).getId());  //活动id
//                //添加记录
//                this.insertGasRecord(userGasRecordVO);
//                //发送消息授权
//                Map<String,Object> gasMap = new HashMap<>();
//                gasMap.put("dtcAddress",dtcAddress);
//                gasMap.put("userId",userId);
//                SendStatus sendStockStatus = stockMqTemplate.syncSend(RocketMqConstant.NFT_RECHARGE_ACTIVITY_TOPIC, new GenericMessage<>(gasMap)).getSendStatus();
//                if (!Objects.equals(sendStockStatus,SendStatus.SEND_OK)) {
//                    log.info("授权消息失败");
//                }
//            }
//        }else{
//            throw new MallcloudException("暂无活动");
//        }
//    }

//    /**
//     * 获取充值活动次数
//     * @return
//     */
//    @Override
//    public List<NftRechargeActivity> getRechargeInfo() {
//        NftRechargeActivityExample example = new NftRechargeActivityExample();
//        NftRechargeActivityExample.Criteria cri = example.createCriteria();
//        cri.andStatusEqualTo(1);
//        List<NftRechargeActivity> nftRechargeActivities = nftRechargeActivityMapper.selectByExample(example);
//        return nftRechargeActivities;
//    }

//    /**
//     * 添加授权错误日志
//     */
//    @Override
//    public void insertRechargeRrror(NftRechargeErrorVO nftRechargeErrorVO){
//        NftRechargeError nftRechargeError = new NftRechargeError();
//        nftRechargeError.setDtcAddress(nftRechargeErrorVO.getDtcAddress());
//        nftRechargeError.setStatus(nftRechargeErrorVO.getStatus());
//        nftRechargeError.setUserId(nftRechargeErrorVO.getUserId());
//        nftRechargeErrorMapper.insertSelective(nftRechargeError);
//    }

//    @Override
//    public List<UserGasRecord> getGasRecordList(String userId, int activity) {
//        UserGasRecordExample example = new UserGasRecordExample();
//        UserGasRecordExample.Criteria cri = example.createCriteria();
//        cri.andActivityEqualTo(activity);
//        cri.andUserIdEqualTo(userId);
//        List<UserGasRecord> userGasRecords = userGasRecordMapper.selectByExample(example);
//        return userGasRecords;
//    }

//    @Override
//    public Map<String, Object> getShowGas(Long userId) {
//        Map<String, Object> map = new HashMap<>();
//        List<NftRechargeActivity> rechargeInfo = getRechargeInfo();
//        if (getRechargeInfo().size() > 0) {
//            //查询是否参与过活动
//            List<UserGasRecord> gasRecordList = getGasRecordList(userId, rechargeInfo.get(0).getId());
//            if (gasRecordList.size() > 0) {
//                map.put("isReceive", false);
//                map.put("gasNum",rechargeInfo.get(0).getActivityNum());
//            }else{
//                map.put("isReceive", true);
//                map.put("gasNum",rechargeInfo.get(0).getActivityNum());
//            }
//        }else{
//            throw new MallcloudException("暂无活动");
//        }
//        return map;
//    }

//    @Override
//    public List<NftRechargeError> getRechargeList() {
//        NftRechargeErrorExample example = new NftRechargeErrorExample();
//        NftRechargeErrorExample.Criteria cri = example.createCriteria();
//        cri.andStatusEqualTo(0);
//        List<NftRechargeError> nftRechargeErrors = nftRechargeErrorMapper.selectByExample(example);
//        return nftRechargeErrors;
//    }
//
//    @Override
//    public void updateRechargeErrorById(Integer id) {
//        NftRechargeError nftRechargeError = new NftRechargeError();
//        nftRechargeError.setStatus(1);
//        nftRechargeError.setId(id);
//        nftRechargeErrorMapper.updateByPrimaryKeySelective(nftRechargeError);
//    }

}
